package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.one.bo.ElectiveBo;
import com.one.dto.PaginationDate;
import com.one.dto.Result;
import com.one.dto.ReturnDate;
import com.one.entity.CourseView;
import com.one.entity.ElectiveClassCourseInfo;
import com.one.entity.ElectiveInfoEntity;
import com.one.entity.SysCourseEntity;
import com.one.enums.IsDeletedEnum;
import com.one.mapper.ElectiveInfoMapper;
import com.one.service.ElectiveInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.utils.DataCheckUtils;
import com.one.utils.DateUtils;
import com.one.utils.PaginationUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-10-14
 */
@Service
public class ElectiveInfoServiceImpl extends ServiceImpl<ElectiveInfoMapper, ElectiveInfoEntity> implements ElectiveInfoService {

    public final ElectiveInfoMapper electiveInfoMapper;

    public ElectiveInfoServiceImpl(ElectiveInfoMapper electiveInfoMapper) {
        this.electiveInfoMapper = electiveInfoMapper;
    }


    @Override
    public Result addElecivr(ElectiveInfoEntity electiveInfoEntity) {
        electiveInfoEntity.setIsDeleted("0");
        electiveInfoEntity.setCreateBy("1");
        electiveInfoEntity.setCreateTime(DateUtils.getCurrentTime());
        int row = electiveInfoMapper.insert(electiveInfoEntity);
        if (row == 0) {
            return Result.error().message("添加课程失败");
        }
        return Result.ok();

    }

    @Override
    public Result selectElective(ElectiveBo ebo) {
        DataCheckUtils.isValid(PaginationUtils.isPagingParameters(ebo.getNumber(), ebo.getPage()), "分页参数错误!");
        int page = ebo.getPage();
        ebo.setPage(PaginationUtils.getPageIndex(ebo.getPage(), ebo.getNumber()));
        List<Object> result= electiveInfoMapper.selectElective(ebo);
        int sum = ((List<Integer>) result.get(1)).get(0);
        //记录总数为0
        if (sum == 0) {
            return Result.error().message("查询结果为空");
        }
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(ebo.getNumber(), page, sum);
        //当前页大于总页数
        if (PaginationUtils.isValid(paginationDate)){
            return Result.error().message("分页参数错误!");
        }
        return Result.ok().data(new ReturnDate(paginationDate,(List<ElectiveClassCourseInfo>) result.get(0) ));

    }

    @Override
    public Result deleteElective(ElectiveInfoEntity electiveInfoEntity) {
        DataCheckUtils.isNotBlank(electiveInfoEntity.getId(), "授课id不能为空!");
        QueryWrapper<ElectiveInfoEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(ElectiveInfoEntity::getId, electiveInfoEntity.getId()).eq(ElectiveInfoEntity::getIsDeleted, IsDeletedEnum.NO.getKey());
        ElectiveInfoEntity elective = electiveInfoMapper.selectOne(sectionQueryWrapper);
        //查询结果校验
        if (elective == null) {
            return Result.error().message("账号不存在！");
        }
        electiveInfoEntity.setIsDeleted(IsDeletedEnum.YES.getKey());
        UpdateWrapper<ElectiveInfoEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ElectiveInfoEntity::getId, electiveInfoEntity.getId()).eq(ElectiveInfoEntity::getIsDeleted,IsDeletedEnum.NO.getKey());
        int row = electiveInfoMapper.update(electiveInfoEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("删除用户失败");
        }
        return Result.ok();
    }

    @Override
    public Result updateElective(ElectiveInfoEntity electiveInfoEntity) {
        electiveInfoEntity.setUpdateBy(DateUtils.getCurrentTime());
        UpdateWrapper<ElectiveInfoEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ElectiveInfoEntity::getId, electiveInfoEntity.getId());
        int row = electiveInfoMapper.update(electiveInfoEntity,updateWrapper);
        if (row == 0) {
            return Result.error().message("修改授课信息失败");
        }
        return Result.ok();
    }
}
